
*Generate two maps of market shares and EV charging shares: 

clear all

*use "$root\Data\EV_alldata_new.dta"
*merge 1:1 stammnummer using "$root\Data\namen\EV_namelist_new.dta"
use "$root//Data/Produced/EV_alldata_PID_new_withKM.dta"

**Add the community identifier: 
merge m:1 PID using "$root/Data/Produced/build_complete.dta", keepusing(GDENR)


frame put VID treibstoffcode GDENR, into(maps_shares)

frame change maps_shares

gen EV=(treibstoffcode=="E")

gcollapse (count) reg_tot=VID (sum) ev_reg=EV, by(GDENR)


*Link to the charging stations: 
frame create EV_charge
frame change EV_charge
use "$root/Data/Original/EV_charge.dta"
keep if year==2019
keep GDENr GDE_stock

collapse (sum) GDE_stock, by(GDENr)

frame change maps_shares
frlink 1:1 GDENR, frame(EV_charge GDENr)
frget _all, from(EV_charge)
drop EV_charge 
frame drop EV_charge

rename GDE_stock EV_charge
gen EV_share_reg=ev_reg*100/reg_tot
replace EV_share_reg=0 if missing(EV_share_reg)
gen Charge_share_reg=EV_charge*100/reg_tot
replace Charge_share_reg=0 if missing(Charge_share_reg)

frame create shape
frame change shape
use "$root/Data/Original/comm.dta"

frlink m:1 BFS_NUMMER, frame(maps_shares GDENR)
frget _all, from(maps_shares)

keep if KANTONSNUM==2


* Now create the graphs: 

set scheme white_tableau


replace EV_share_reg=round(EV_share_reg,.01)

colorpalette viridis, n(10) nograph reverse gscale
local colors `r(p)'


*Plot the EV_share in the registrations: 
#delim ;
spmap EV_share_reg using "$root/Data/Original/comm_coord.dta", id(_ID) clmethod(quantile) clnumber(10) fcolor("`colors'") mocolor(gs2 ) ndfcolor(white) ndocolor(gs2 ..) ndsize(0.03 ..) ndlabel("No data")  ocolor(gs2 ..) osize(0.03 ..) 
 legend(pos(2) size(2))  legtitle("EV fleet share(%) ")
label(data("$root/Data/Original/geodata/PLZ_labels.dta") x(_CX) y(_CY) label(PLZName) color(black) size(small) gap(.1cm) position(3)) 
point(data("$root/Data/Original//PLZ_labels.dta") select(drop if PLZName=="") x(_CX) y(_CY) shape(circle) fcolor(maroon) ocolor(maroon) size(tiny))
polygon(data("$root/Data/Original/seen_coords_bern.dta") fcolor(gs16*0.5) ocolor(gs16*0.5) osize(vthin)) 
;
#delim cr

graph export "$root/Results/tables_graphs/main/map_EVshare_reg.png", replace


*Plot the EV charing share per car: 

replace Charge_share_reg=round(Charge_share_reg,.01)

colorpalette viridis, n(10) nograph reverse gscale
local colors `r(p)'


*Plot the EV_share in the registrations: 
#delim ;
spmap Charge_share_reg using "$root/Data/Original/comm_coord.dta", id(_ID) clmethod(quantile) clnumber(10) fcolor("`colors'") mocolor(gs2 ) ndfcolor(white) ndocolor(gs2 ..) ndsize(0.03 ..) ndlabel("No data")  ocolor(gs2 ..) osize(0.03 ..) 
 legend(pos(2) size(2))  legtitle("Public charging (per 100 cars) ")
label(data("$root/Data/Original//PLZ_labels.dta") x(_CX) y(_CY) label(PLZName) color(black) size(small) gap(.1cm) position(3)) 
point(data("$root/Data/Original//PLZ_labels.dta") select(drop if PLZName=="") x(_CX) y(_CY) shape(circle) fcolor(maroon) ocolor(maroon) size(tiny))
polygon(data("$root/Data/Original/seen_coords_bern.dta") fcolor(gs16*0.5) ocolor(gs16*0.5) osize(vthin)) 
;
#delim cr

graph export "$root/Results/tables_graphs/main/map_ChargeShare_reg.png", replace


**Generate the household point data for the map in the appendix

preserve
use "$root/Data/Produced/EV_sample.dta"", clear
keep if choice==1
keep if drivetype==3 | drivetype==4

keep drivetype GKODX GKODY
duplicates drop drivetype GKODX GKODY, force

gen  _CX=2000000+GKODX
gen _CY= GKODY +1000000
tempfile file
save "$root/Data/Original/HH_points.dta", replace
restore


colorpalette viridis, n(3) nograph  

#delim ;
spmap using "$root/Data/Original/comm_coord.dta", id(_ID) clmethod(unique) clnumber(2) fcolor(gs15) mocolor(gs2 ) legenda(off) legen(position(2))  ocolor(gs2 ..) osize(0.03 ..) 
point(data("$root/Data/Original/HH_points.dta") size("tiny" "tiny") 
x(_CX) y(_CY) by(drivetype) legenda(on) legtitle("Vehicle type") fcolor(`r(p)'))
polygon(data("$root/Data/Original/seen_coords_bern") fcolor(ltblue*.8) ocolor(ebblue*.7) osize(vthin))
point(data("$root/Data/Original//PLZ_labels.dta") select(drop if PLZName=="") x(_CX) y(_CY) shape(circle) fcolor(maroon) ocolor(maroon) size(tiny))
label(data("$root/Data/Original/PLZ_labels") x(_CX) y(_CY) label(PLZName) color(maroon) size("vsmall") position(3)) 
;
#delim cr
graph export "$root/Results/tables_graphs/appendix/map_drivetype.png", replace



